let label = "全部"
let navigation = "全部"
let color = "#FCAFA2"
let data0

let label1 = $($(".label")[0])
let NavigationTitle1 = $($(".NavigationTitle")[0])
let color0 = $($(".color0")[0])


$.ajax({
    type: "post",
    url: "http://localhost:8080/MessageWallAll",
    data: {},
    dataType: "json",
    headers: {
        token: sessionStorage.getItem('token')
    },
    success: function (data) {
        if (data.code === 0) {
            console.log(11)
            location.href = "http://localhost:8080/"
        }
        data0 = data
        console.log(data);
        let a
        for (let i = 0; i < data.length; i++) {
            if (label === navigation) {
                a = "<div class='Singlemessage' style='background:" + data[i].color + "'value='" + data[i].id + "'><div class='div3142'><div class='blue0'></div><div class='red0'></div></div><div class='date'>" + data[i].date + "</div><div class='message0'>留言</div><div class='Textcontent'>" + data[i].message + "</div><div class='Messagerecipient'>" + data[i].name + "</div></div>"
            }
            $("#Messagecollection").append(a)
        }
        /*        $(".Singlemessage").each(function (index, app) {
                    console.log(this)
                    console.log($(this).attr("value"))
                })*/
    }
})


label1.css("background", "#EBEBEB")
label1.children("span").css("color", "#202020")
NavigationTitle1.css({"border": "1px solid rgba(32, 32, 32, 1)", "border-radius": "14px"})
NavigationTitle1.children("span").css({
    "font-family": "PingFangSC-Semibold",
    "color:": "#202020;",
    "font-weight": "600"
})
color0.css("border", "1px solid rgba(59, 115, 240, 1)")

$(".label").click(function () {
    let a = $(this)
    $(".label").css("background", "none")
    a.css("background", "#EBEBEB")
    $(".span0").css("color", "#5B5B5B")
    $(a.children("span")).css("color", "#202020")
    label = $(a.children("span")).text()
    console.log(label)
})

$(".NavigationTitle").click(function () {
    let a = $(this)
    $(".NavigationTitle").css("border", "none")
    a.css({"border": "1px solid rgba(32, 32, 32, 1)", "border-radius": "14px"})
    $(".NavigationTitle0").css({"font-family": "PingFangSC-Regular", "color": "#5B5B5B", "font-weight": "400"})
    $(a.children("span")).css({"font-family": "PingFangSC-Semibold", "color:": "#202020;", "font-weight": "600"})
    navigation = $(a.children("span")).text()
    console.log(navigation)
    let array = new Array()
    if (navigation == "全部") {
        $(".Singlemessage").each(function () {
            if (array.indexOf(parseInt($(this).attr("value"))) == -1) {
                $(this).css("display", "block")
            }
        })
    } else {
        for (let i = 0; i < data0.length; i++) {
            if (data0[i].label === navigation || data0[i].label == "全部") {
                array.push(data0[i].id)
            }
        }
        $(".Singlemessage").each(function () {
            if (array.indexOf(parseInt($(this).attr("value"))) == -1) {
                $(this).css("display", "none")
            } else {
                $(this).css("display", "block")
            }
        })
    }
    console.log(array)
})

$(".color0").click(function () {
    let a = $(this)
    $(".color0").css("border", "none")
    a.css("border", "1px solid rgba(59, 115, 240, 1)")
    let colorz = a.attr("value")
    $("#Messageboard").css("background", colorz)
    $("#content").css("background", colorz)
    $(".textname").css("background-color", colorz)
    color = colorz
    console.log(color)
})

$("#Confirm").click(function () {

    if (confirm("确定添加？")) {
        let a0 = $("#content").val()
        let a1 = $(".textname").val()
        let a
        let myDate = new Date;
        let year = myDate.getFullYear(); //获取当前年
        let mon = myDate.getMonth() + 1; //获取当前月
        let date = myDate.getDate();
        let Date0 = year + "." + mon + "." + date
        $("#Messagecollection").append(a)
        $.ajax({
            type: "post",
            url: "http://localhost:8080/InsertMessage",
            data: {
                message: a0,
                color: color,
                date: Date0,
                name: a1,
                label: label
            },
            headers: {
                token: sessionStorage.getItem('token')
            },
            success: function (data) {
                console.log(data.code)
                if (data.code === 0) {
                    console.log(11)
                    location.href = "http://localhost:8080/"
                }
                data0 = data
                console.log(data0)
                if (data != null) {
                    if (label == navigation || navigation == "全部" || label == "全部") {
                        a = "<div class='Singlemessage' style='background:" + color + "'value='" + data0[data0.length - 1].id + "'><div class='div3142'><div class='blue0'></div><div class='red0'></div></div><div class='date'>" + Date0 + "</div><div class='message0'>留言</div><div class='Textcontent'>" + a0 + "</div><div class='Messagerecipient'>" + a1 + "</div></div>"
                    } else {
                        a = "<div class='Singlemessage' style='background:" + color + "; display: none;' value='" + data0[data0.length - 1].id + "'><div class='div3142'><div class='blue0'></div><div class='red0'></div></div><div class='date'>" + Date0 + "</div><div class='message0'>留言</div><div class='Textcontent'>" + a0 + "</div><div class='Messagerecipient'>" + a1 + "</div></div>"
                    }
                    $("#Messagecollection").append(a)
                    $("#discard").trigger("click");
                }
            }
        })
    }
})


$("#discard").click(function () {
    label = "全部"
    $(".label").css("background", "none")
    label1.css("background", "#EBEBEB")
    $(".span0").css("color", "#5B5B5B")
    label1.children("span").css("color", "#202020")
    $(".color0").css("border", "none")
    color0.css("border", "1px solid rgba(59, 115, 240, 1)")
    color = "#FCAFA2"
    $("#Messageboard").css("background", color)
    $("#content").css("background", color)
    $(".textname").css("background-color", color)
    $("#content").val("")
    $(".textname").val("")
})

$(document).on("mouseover", ".div3142", function () {
    $($(this).children("div")).css("background", "black")
})

$(document).on("mouseout", ".div3142", function () {
    $($($(this).children("div"))[0]).css("background", "#3B73F0")
    $($($(this).children("div"))[1]).css("background", "#F67770")
})

$(document).on("click", ".div3142", function () {
    let id = $($(this).parent("div")[0]).attr("value")
    if (confirm("确定撕掉？")) {
        $($(this).parent("div")[0]).remove()
        $.ajax({
            type: "post",
            url: "http://localhost:8080/Deletemessage",
            data: {
                id: id
            },
            headers: {
                token: sessionStorage.getItem('token')
            },
            success: function (data) {
                if (data.code === 0) {
                    console.log(11)
                    location.href = "http://localhost:8080/"
                }
                data0 = data
                console.log(data0)
            }
        })
    }
})


